<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.query.account" placeholder="账号" style="width: 200px;" class="filter-item" @keyup.enter.native="filter" />
      <el-input v-model="listQuery.query.name" placeholder="姓名" style="width: 200px;" class="filter-item" @keyup.enter.native="filter" />
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="filter">
        搜索
      </el-button>
      <el-button v-permission="'/user/user/add'" class="filter-item" style="margin-left: 10px;" type="primary" @click="create">
        添加
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      @sort-change="sortChange"
    >
      <el-table-column prop="id" align="center" label="ID" width="95" sortable>
        <template v-slot="{row}">
          {{ row.id }}
        </template>
      </el-table-column>
      <el-table-column prop="account" label="账号" width="150" sortable>
        <template v-slot="{row}">
          {{ row.account }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="150" sortable>
        <template v-slot="{row}">
          {{ row.name }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="角色" width="300">
        <template v-slot="{row}">
          <el-tag v-for="role in row.roles" :key="role.id" style="margin-left: 5px;" type="info">
            {{ role.role.desc }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="锁定" align="center" width="170" sortable>
        <template v-slot="{row}">
          <el-switch
            v-model="row.locked"
            style="display: block"
            active-color="#ff4949"
            inactive-color="#13ce66"
            active-text="已锁定"
            inactive-text="未锁定"
            @change="locking(row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="头像" align="center" show-overflow-tooltip>
        <template v-slot="{row}">
          <span>{{ row.avatar }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="200" align="center">
        <template v-slot="{row}">
          <span> {{ row.updatedAt | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }} </span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="200">
        <template v-slot="{row}">
          <span> {{ row.createdAt | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }} </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="180">
        <template v-slot="{row}">
          <el-button v-permission="'/user/user/update'" type="primary" size="small" @click="edit(row.id)">编辑</el-button>
          <el-button v-permission="'/user/user/changeCode'" :disabled="row.code === 'ldap'" type="warning" size="small" @click="changeCode(row.id)">修改密码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="listQuery.totalRecords>0" :total="listQuery.totalRecords" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="fetchData" />
  </div>
</template>

<script>
import { list, locking, changeCode } from '@/api/user'
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        totalRecords: 0,
        orders: undefined,
        query: {
        }
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      list(this.listQuery).then(response => {
        this.list = response.data.list
        this.listQuery.totalRecords = response.data.page.totalRecords
        this.listLoading = false
      })
    },
    filter() {
      this.listQuery.pageNum = 1
      this.fetchData()
    },
    create() {
      this.$router.push('/manage/users/edit_user')
    },
    edit(id) {
      this.$router.push('/manage/users/edit_user?id=' + id)
    },
    locking(user) {
      let confirmTxt
      let messageTxt
      if (user.locked) {
        confirmTxt = '是否锁定用户 ' + user.name + ' ?'
        messageTxt = '用户锁定成功!'
      } else {
        confirmTxt = '是否解锁用户 ' + user.name + ' ?'
        messageTxt = '用户解锁成功!'
      }

      this.$confirm(confirmTxt, '提示', {
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'warning'
      }).then(() => {
        locking({ id: user.id, locked: user.locked }).then(response => {
          this.$message({ message: messageTxt, type: 'success' })
        })
      }).catch(() => {
        user.locked = !user.locked
      })
    },
    // 修改用户密码
    changeCode(id) {
      this.$prompt('请输入新密码', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^.{6,50}$/,
        inputErrorMessage: '密码 长度应在 6 到 50 个字符之间'
      }).then(({ value }) => {
        changeCode({ id: id, code: value }).then(response => {
          this.$message({ message: '密码修改成功过！', type: 'success' })
        })
      }).catch(() => {
      })
    },
    sortChange(column) {
      this.listQuery.orders = [{
        column: column.prop,
        asc: column.order === 'ascending'
      }]
      this.fetchData()
    }
  }
}
</script>
